package com.jiudu.gulimall.product.service.impl;

import com.jiudu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiudu.common.utils.PageUtils;
import com.jiudu.common.utils.Query;

import com.jiudu.gulimall.product.dao.CategoryDao;
import com.jiudu.gulimall.product.entity.CategoryEntity;
import com.jiudu.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService brandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {

        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
               categoryEntity.getParentCid() == 0).map(categoryEntity -> {
                   categoryEntity.setChildren(getChildren(categoryEntity,entities));
                   return categoryEntity;
        }).sorted((categoryEntity1,categoryEntity2)->
                categoryEntity1.getSort() - categoryEntity2.getSort()
        ).collect(Collectors.toList());
        /**
         * 如果菜单的Sort属性为空则需要判断
        (categoryEntity1.getSort() ==null ? 0:categoryEntity1.getSort())
                - (categoryEntity2.getSort() ==null ? 0:categoryEntity2.getSort())
         */
        return level1Menus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO  检查当前删除的菜单是否被别的地方所引用
        baseMapper.deleteBatchIds(asList);
    }

    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        brandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    public List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {

        List<CategoryEntity> list = all.stream().filter(categoryEntity ->
            categoryEntity.getParentCid().equals(root.getCatId())
        ).map(categoryEntity -> {
           categoryEntity.setChildren(getChildren(categoryEntity,all));
           return categoryEntity;
        }).sorted( (categoryEntity1,categoryEntity2) ->
                        categoryEntity1.getSort() - categoryEntity2.getSort()
                ).collect(Collectors.toList());

        return list;
    }
}